# __*__ coding:utf-8 __*__
import time
import autoit

from qturboframe.lib.utils.windows.win_control import WinControl
from qturboframe.lib.report.log import logger
from qturboframe.lib.utils.CustomError.custom_error import CustomError
from qturboframe.lib.utils.CustomError.custom_exception import CustomException

def download_file_from_chrome(file, win_name = "保存文件", TIMEOUT=3):
    wc = WinControl(win_name)
    ex_msg = ""
    for i in range(4):
        try:
            wc.control_focus("Edit1")
            break
        except Exception as e:
            ex_msg = str(e)
    else:
        raise CustomException(CustomError.Error_EQ03003000_Window_Control_Not_Found, str(ex_msg))
    wc.wait(2)
    # 下载文件命名
    logger.debug("选择文件")
    wc.control_settext("Edit1", file)
    time.sleep(TIMEOUT)
    # 点击打开按钮
    logger.debug("点击打开按钮")
    i = 0
    while i < 3:
        try:
            if wc.exists():
                wc.control_click("Button2")
                break
            else:
                i += 1
                continue
        except autoit.AutoItError as ex:
            raise ex


if __name__ == '__main__':
    download_file_from_chrome(r'所有执行_20210127.csv',win_name="另存为")

